package com.hks360.car_treasure.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.model.Add.User;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenter;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenterImpl;
import com.hks360.car_treasure.mvp.user.view.UserView;
import com.hks360.car_treasure.util.TimerUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.UIUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * 找回密码
 */
public class FindPassActivity extends BaseActivity implements UserView{
    private EditText mPhoneEt;
    private TextView mGetCodeTv;
    private EditText mCodeEt;
    private EditText mNewPassEt;
    private EditText mNewPassAgainEt;
    private TextView mFindPassTv;

    private UserPresenter mUserPresenter;

    @Override
    public void initView() {
        initTitleBar(R.string.find_pass_title);
        initDialog(R.string.loading_submit);
        setupView();
        initData();
        addListener();
    }

    @Override
    public void addListener() {
        mGetCodeTv.setOnClickListener(this);
        mFindPassTv.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mUserPresenter = new UserPresenterImpl(this,this);
    }

    @Override
    public void setupView() {
        mNewPassEt = UIUtil.findViewById(this, R.id.findpass_newpass_et);
        mNewPassAgainEt = UIUtil.findViewById(this,R.id.findpass_newpass_again_et);
        mPhoneEt = UIUtil.findViewById(this, R.id.findpass_phone_et);
        mCodeEt = UIUtil.findViewById(this, R.id.findpass_code_et);
        mGetCodeTv = UIUtil.findViewById(this, R.id.findpass_getcode_tv);
        mFindPassTv = UIUtil.findViewById(this, R.id.findpass_tv);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_findpass);
        initView();
    }

    public boolean isInputError() {
        if (CommonUtil.isEmptyEt(mPhoneEt) || CommonUtil.isEmptyEt(mNewPassEt) || CommonUtil.isEmptyEt(mCodeEt)) {
            CommonUtil.showToast(this, "请您把信息填写完整后再按确定按钮");
            return true;
        }
        if (mNewPassEt.getText().toString().length() > 20 || mNewPassEt.getText().toString().length() < 6) {
            CommonUtil.showToast(this, "请您按提示输入密码位数");
            return true;
        }
        if(!mNewPassEt.getText().toString().trim().equals(mNewPassAgainEt.getText().toString().trim())) {
            CommonUtil.showToast(this,"两次密码输入不一致");
            return true;
        }
        return false;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.findpass_getcode_tv:
                if (!CommonUtil.isEmptyEt(mPhoneEt)) {
                    mGetCodeTv.setEnabled(false);
                    mUserPresenter.sendCheckCode(R.id.findpass_getcode_tv);
                } else {
                    CommonUtil.showToast(this, "请先输入您的手机号再发送验证码请求");
                }
                break;
            case R.id.findpass_tv:
                if (!isInputError()) {
                    TimerUtil.timerCancel();
                    mGetCodeTv.setHint("获取验证码");
                    mGetCodeTv.setEnabled(true);
                    mUserPresenter.changePassword(R.id.findpass_tv);
                }
                break;
        }

    }

    @Override
    public void clearView() {
        mPhoneEt.setText(null);
        mNewPassEt.setText(null);
        mCodeEt.setText(null);
    }

    @Override
    public void sendCheckCodeFailed() {

    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.dismiss();
    }

    @Override
    public String getMobile() {
        return mPhoneEt.getText().toString().trim();
    }

    @Override
    public String getCode() {
        return mCodeEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mNewPassEt.getText().toString().trim();
    }

    @Override
    public void loginSuccess(User user) {

    }

    @Override
    public void showToastMsg(String msg) {
        CommonUtil.showToast(this,msg);
    }

    @Override
    public void sendCheckCodeSuccess() {
        TimerUtil.createTimer(60000, 1000, mGetCodeTv);
    }

    @Override
    public void findPasswordSuccess() {
        finish();
    }
}
